package com.lhy.controller;

import com.lhy.domain.Registerorder;
import com.lhy.service.RegisterorderService;
import com.lhy.utils.Pagination;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 汪
 */
@Controller
public class RegisterorderController {
    @Autowired
    RegisterorderService registerorderService;
    @RequestMapping("/deleteRegisterorder")
    public String deleteRegisterorder(HttpServletRequest req, HttpServletResponse resp) {
        Integer roId = Integer.parseInt(req.getParameter("roId"));
        registerorderService.deleteRegisterorder(roId);
        return "main";
    }

    @RequestMapping("/addRegisterorder")
    public String addRegisterorder(HttpServletRequest req, HttpServletResponse resp) {
        Registerorder registerorder = new Registerorder();
        registerorder.setOpTime(new Date());
        DateConverter converter = new DateConverter();
        converter.setPattern(new String("yyyy-MM-dd"));
        ConvertUtils.register(converter, Date.class);
        Map<String, String[]> map = req.getParameterMap();
        try {
            BeanUtils.populate(registerorder, map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        registerorderService.addRegisterorder(registerorder);
        return "main";
    }

    @RequestMapping("/findSomeRegisterorder")
    public String findSomeRegisterorder(Registerorder registerorder, Pagination<Registerorder> page,HttpServletRequest req, HttpServletResponse resp) {
        page.setQueryObject(registerorder);
        registerorderService.findSomeRegisterorder(page);
        HttpSession session = req.getSession();
        session.setAttribute("registerorders", page);
        return "ghlist";
    }

}
